/**
 * UserPanel.java
 *
 * Created on 25-mar-2010, 16.12.55
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */
package unibo.lsb.graphic;

import java.awt.Color;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

import unibo.lsb.logic.Player;
import unibo.lsb.logic.PlayersTable;

/**
 * Single user panel, holding information on a player
 */
public class UserPanel extends javax.swing.JPanel {

	private javax.swing.JLabel lbl_penalties;
	private javax.swing.JLabel lbl_playerIcon;
	private javax.swing.JLabel lbl_playerName;

	/* size and any other argument has to be specified here */
	String playerName = "";
	Icon playerIcon = null;
	String penalties = "0";
	boolean dead = false;
	boolean lost = false;

	/**
	 * Constructor
	 * 
	 * @param playerName
	 * @param playerIcon
	 * @param penalties
	 */
	public UserPanel(String playerName, Icon playerIcon, String penalties) {
		this.playerName = playerName;
		this.playerIcon = playerIcon;
		this.penalties = penalties;
		initComponents();
	}

	/**
	 * Constructor
	 * 
	 * @param player
	 */
	public UserPanel(Player player) {
		retrievePlayerInformation(player);
		initComponents();
	}

	/**
	 * Empty constructor
	 */
	public UserPanel() {
		initComponents();
	}

	/**
	 * Updates the panel with the player information
	 * 
	 * @param player
	 */
	public void updatePanel(Player player) {
		retrievePlayerInformation(player);
		lbl_penalties.setText(penalties);
		lbl_playerIcon.setIcon(playerIcon);
		lbl_playerName.setText(playerName);
		if (dead) {
			this.setDead();
		}
		if (lost) {
			this.setLost();
		}
		repaint();
	}

	/**
	 * Only repaints the panel
	 */
	public void updatePanel() {
		repaint();
	}

	/**
	 * Initialization
	 */
	private void initComponents() {

		lbl_playerName = new javax.swing.JLabel();
		lbl_playerIcon = new javax.swing.JLabel();
		lbl_penalties = new javax.swing.JLabel();

		setMaximumSize(new java.awt.Dimension(140, 56));
		setMinimumSize(new java.awt.Dimension(140, 56));
		setPreferredSize(new java.awt.Dimension(140, 56));

		this.setBackground(java.awt.Color.white);
		this.setBorder(new javax.swing.border.SoftBevelBorder(
				javax.swing.border.BevelBorder.RAISED, null,
				java.awt.Color.white, null, null));
		this.setMaximumSize(new java.awt.Dimension(140, 56));
		this.setMinimumSize(new java.awt.Dimension(140, 56));

		lbl_playerName
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lbl_playerName.setText(playerName);
		lbl_playerName.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		lbl_playerName.setMaximumSize(new java.awt.Dimension(96, 25));
		lbl_playerName.setMinimumSize(new java.awt.Dimension(96, 25));
		lbl_playerName.setPreferredSize(new java.awt.Dimension(96, 25));

		lbl_playerIcon.setText("");
		lbl_playerIcon.setIcon(playerIcon);
		lbl_playerIcon.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		lbl_playerIcon.setMaximumSize(new java.awt.Dimension(32, 32));
		lbl_playerIcon.setMinimumSize(new java.awt.Dimension(32, 32));
		lbl_playerIcon.setPreferredSize(new java.awt.Dimension(32, 32));

		lbl_penalties.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		lbl_penalties.setText(penalties);
		lbl_penalties.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		lbl_penalties.setMaximumSize(new java.awt.Dimension(96, 25));
		lbl_penalties.setMinimumSize(new java.awt.Dimension(96, 25));
		lbl_penalties.setPreferredSize(new java.awt.Dimension(96, 25));

		javax.swing.GroupLayout pnl_userPanelLayout = new javax.swing.GroupLayout(
				this);
		this.setLayout(pnl_userPanelLayout);
		pnl_userPanelLayout
				.setHorizontalGroup(pnl_userPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnl_userPanelLayout
										.createSequentialGroup()
										.addGroup(
												pnl_userPanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																lbl_playerName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lbl_penalties,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										// .addPreferredGap(
										// javax.swing.LayoutStyle.ComponentPlacement.RELATED
										// )
										.addComponent(
												lbl_playerIcon,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)));
		pnl_userPanelLayout
				.setVerticalGroup(pnl_userPanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnl_userPanelLayout
										.createSequentialGroup()
										.addComponent(
												lbl_playerName,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(0, 0, 0)
										.addComponent(
												lbl_penalties,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGroup(
								pnl_userPanelLayout
										.createSequentialGroup()
										.addGap(8, 8, 8)
										.addComponent(
												lbl_playerIcon,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)));

		// javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

		Runnable r = new Runnable() {

			public void run() {
				repaint();
			}
		};
		SwingUtilities.invokeLater(r);
	}

	/**
	 * Private method that retrieves information of the given Player relevant
	 * for the panel, and saves them in class local variable.
	 * 
	 * @param player
	 */
	private void retrievePlayerInformation(Player player) {
		this.playerName = player.getPlayerId().getName();
		this.penalties = (new Integer(player.getPenalty())).toString();
		String iconLocalPath = ConfigurationConstGraphics.ICON_PATH
				+ player.getPlayerId().getIconNumber()
				+ ConfigurationConstGraphics.ICON_EXT;
		URL iconUrl = getClass().getResource(iconLocalPath);
		this.playerIcon = new ImageIcon(iconUrl);
		if (player.getStatus() != player.ALIVE) {
			this.dead = true;
		}
		if (player.getPenalty() == PlayersTable.MAX_PENALTIES) {
			this.lost = true;
		}

	}

	/**
	 * Set the number of penalties to the new value
	 * 
	 * @param penalties
	 */
	public void setPenalties(String penalties) {
		this.penalties = penalties;
		lbl_penalties.setText(penalties);
		revalidate();
		repaint();
	}

	/**
	 * Borders the panel with a green highlight to show the player holds the
	 * turn.
	 */
	public void setTurnToPlayer() {
		this.setBorder(javax.swing.BorderFactory.createLineBorder(Color.GREEN,
				5));
		revalidate();
		repaint();
	}

	/**
	 * Removes green border that indicates turn.
	 */
	public void unsetTurnToPlayer() {
		this.setBorder(javax.swing.BorderFactory.createEmptyBorder());
		revalidate();
		repaint();
	}

	public void setDead() {
		dead = true;
		this.setBackground(new Color(145, 150, 161));
		lbl_penalties.setText("DEAD");
		revalidate();
		repaint();
	}

	public void setLost() {
		lost = true;
		this.setBackground(new Color(161, 130, 125));
		lbl_penalties.setForeground(Color.red);
		revalidate();
		repaint();
	}
}
